package com.healthcloud.controller.modules.PatientRecord;

import cn.hutool.core.util.StrUtil;
import com.healthcloud.entity.HlDoctorEntity;
import com.healthcloud.entity.Hospital;
import com.healthcloud.entity.MsgVo;
import com.healthcloud.entity.PatientRecord.MedicalCheckEntity;
import com.healthcloud.entity.PatientRecord.MedicalProjectEntity;
import com.healthcloud.entity.PatientRecord.MedicalTestEntity;
import com.healthcloud.entity.TestResult;
import com.healthcloud.service.HlDoctorService;
import com.healthcloud.service.HospitalService;
import com.healthcloud.service.PatientRecord.MedicalCheckService;
import com.healthcloud.service.PatientRecord.MedicalProjectService;
import com.healthcloud.service.PatientRecord.MedicalTestService;
import com.healthcloud.service.TestResultService;
import com.healthcloud.utils.ApiRes;
import com.healthcloud.utils.PageParam;
import com.healthcloud.utils.ShiroUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.Array;
import java.util.*;

/**
 * Created by admin on 2018/8/30.
 */
@Controller
@RequestMapping("testProject")
public class TestProjectController {

    @Autowired
    private MedicalCheckService medicalCheckService;

    @Autowired
    private MedicalTestService medicalTestService;

    @Autowired
    private HlDoctorService hlDoctorService;

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private MedicalProjectService medicalProjectService;

    @Autowired
    private TestResultService testResultService;

    /**
     * 查询检查项目信息
     * @param entity
     * @param pageParam
     * @return
     */
    @ApiOperation(value = "查询检查项目信息", notes = "查询检查项目信息")
    @RequestMapping(value = "/checkInfo", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes checkInfo(MedicalCheckEntity entity, PageParam pageParam) {
        List<MedicalCheckEntity> list = medicalCheckService.queryByIdCard(entity, pageParam);
        int total = medicalCheckService.countByIdCard(entity);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("iTotalDisplayRecords", total);
        return new ApiRes(true, map, "", "");
    }


    /**
     * 跳转至检查项目新增页面
     */
    @ApiOperation(value = "检查项目新增页面", notes = "检查项目新增页面")
    @RequestMapping(value = "/toAddCheck", method = RequestMethod.GET)
    public String toAddCheck() {
        return "admin/archives/addCheck";
    }


    /**
     * 新增检查项目信息
     * @param entity
     * @return
     */
    @ApiOperation(value = "新增检查项目信息", notes = "新增检查项目信息")
    @RequestMapping(value = "/addCheck", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes addCheck(MedicalCheckEntity entity) {
        HlDoctorEntity doctor = hlDoctorService.queryByUserId(ShiroUtils.getUserId());
        entity.setHospitalId(doctor == null ? null : doctor.getHospId());
        entity.setHospitalName(doctor.getHospName());
        entity.setDoctorId(doctor == null ? null : doctor.getId());
        entity.setDoctorName(doctor.getDrName());
        entity.setType(1);
        medicalCheckService.save(entity);
        return ApiRes.SUCCESS;
    }


    /**
     * 跳转至检查项目修改页面
     */
    @ApiOperation(value = "检查项目修改页面", notes = "检查项目修改页面")
    @RequestMapping(value = "/toUpdateCheck", method = RequestMethod.GET)
    public String toUpdateCheck() {
        return "admin/archives/updateCheck";
    }


    /**
     * 修改检查项目信息
     * @param entity
     * @return
     */
    @ApiOperation(value = "修改检查项目信息", notes = "修改检查项目信息")
    @RequestMapping(value = "/updateCheck", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes updateCheck(MedicalCheckEntity entity) {
        MedicalCheckEntity medicalCheckEntity = medicalCheckService.queryObject(entity.getId());
        //医生和医院都没有记录
        if(medicalCheckEntity.getDoctorName() == null && medicalCheckEntity.getHospitalId() == null) {
            HlDoctorEntity doctor = hlDoctorService.queryByUserId(ShiroUtils.getUserId());
            //当前用户为医生账号
            if(doctor != null) {
                entity.setDoctorId(doctor.getId());
                entity.setHospitalId(doctor.getHospId());
            }
        }
        medicalCheckService.update(entity);
        return ApiRes.SUCCESS;
    }


    /**
     * 删除检查项目信息
     * @param id 序号
     * @return
     */
    @ApiOperation(value = "删除检查项目信息", notes = "删除检查项目信息")
    @RequestMapping(value = "/deleteCheck", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes deleteCheck(@ApiParam(required = true, name = "id", value = "序号")
                              @RequestParam("id") Integer id) {
        medicalCheckService.delete(id);
        return ApiRes.SUCCESS;
    }


    /**
     * 跳转至检查项目更多页面
     */
    @ApiOperation(value = "检查项目更多页面", notes = "检查项目更多页面")
    @RequestMapping(value = "/toMoreCheck", method = RequestMethod.GET)
    public String toMoreCheck() {
        return "admin/archives/moreCheck";
    }


    /**
     * 查询检验项目信息
     * @param entity
     * @param pageParam
     * @return
     */
    @ApiOperation(value = "查询检验项目信息", notes = "查询检验项目信息")
    @RequestMapping(value = "/testInfo", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes testInfo(MedicalTestEntity entity, PageParam pageParam) {
        List<MedicalTestEntity> list = medicalTestService.queryByIdCard(entity, pageParam);
        int total = medicalTestService.countByIdCard(entity);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("iTotalDisplayRecords", total);
        return new ApiRes(true, map, "", "");
    }

    /**
     * 查询检验项目信息
     * @param entity
     * @param pageParam
     * @return
     */
    @ApiOperation(value = "查询检验项目信息", notes = "查询检验项目信息")
    @RequestMapping(value = "/testInfo/new", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes testInfoNew(MedicalTestEntity entity, PageParam pageParam) {

        List<TestResult> testResults =  testResultService.findByIdCard(entity.getIdCard(),pageParam);

        List<MedicalTestEntity> list = new ArrayList<>();
        testResults.forEach(testResult1 -> {

            MedicalTestEntity medicalTestEntity = new MedicalTestEntity();
            //大类项目
            medicalTestEntity.setLagerProjectName(testResult1.getTestKindName());
            //小类项目
            medicalTestEntity.setSmallProjectName(testResult1.getTestItemName());
            //结果
            medicalTestEntity.setTestResult(testResult1.getTestValue());
            //参考值
            medicalTestEntity.setReferenceValue("");
            //单位
            medicalTestEntity.setUnit(testResult1.getValueUnit() == null ? "":testResult1.getValueUnit());
            //检验时间
            medicalTestEntity.setCreateTime(testResult1.getCreateTime()== null ? null: testResult1.getCreateTime());
            //创建医生
            medicalTestEntity.setDoctorName(testResult1.getInspectorName() == null ? "": testResult1.getInspectorName());
            //所属医院
            medicalTestEntity.setHospitalName(testResult1.getInspectionDept() == null ? "": testResult1.getInspectionDept());

            list.add(medicalTestEntity);


        });

//        List<MedicalTestEntity> list = medicalTestService.queryByIdCard(entity, pageParam);
//        int total = medicalTestService.countByIdCard(entity);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("iTotalDisplayRecords", testResultService.findCountByIdCard(entity.getIdCard()));
        return new ApiRes(true, map, "", "");
    }


    /**
     * 跳转至检验项目新增页面
     */
    @ApiOperation(value = "检验项目新增页面", notes = "检验项目新增页面")
    @RequestMapping(value = "/toAddTest", method = RequestMethod.GET)
    public String toAddTest() {
        return "admin/archives/addTest";
    }


    /**
     * 新增检验项目信息
     * @param entity
     * @return
     */
    @ApiOperation(value = "新增检验项目信息", notes = "新增检验项目信息")
    @RequestMapping(value = "/addTest", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes addTest(MedicalTestEntity entity) {
        HlDoctorEntity doctor = hlDoctorService.queryByUserId(ShiroUtils.getUserId());
        List<Hospital> hospitals = hospitalService.getHospitalsByUserId(ShiroUtils.getUserId());
        if (hospitals!=null && hospitals.size()>0){
            entity.setHospitalName(hospitals.get(0).getHospName());
        }
        entity.setDoctorId(doctor == null ? null : doctor.getId());
        entity.setHospitalId(doctor == null ? null : doctor.getHospId());
        entity.setDoctorName(doctor.getDrName());
        entity.setType(1);
        medicalTestService.save(entity);
        return ApiRes.SUCCESS;
    }


    /**
     * 跳转至检验项目修改页面
     */
    @ApiOperation(value = "检验项目修改页面", notes = "检验项目修改页面")
    @RequestMapping(value = "/toUpdateTest", method = RequestMethod.GET)
    public String toUpdateTest() {
        return "admin/archives/updateTest";
    }


    /**
     * 修改检验项目信息，若该记录没有建档医生和医院则改为当前用户的信息
     * @param entity
     * @return
     */
    @ApiOperation(value = "修改检验项目信息", notes = "修改检验项目信息")
    @RequestMapping(value = "/updateTest", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes updateTest(MedicalTestEntity entity) {
        MedicalTestEntity medicalTestEntity = medicalTestService.queryObject(entity.getId());
        //医生和医院都没有记录
        if(medicalTestEntity.getDoctorId() == null && medicalTestEntity.getHospitalId() == null) {
            HlDoctorEntity doctor = hlDoctorService.queryByUserId(ShiroUtils.getUserId());
            //当前用户为医生账号
            if(doctor != null) {
                entity.setDoctorId(doctor.getId());
                entity.setHospitalId(doctor.getHospId());
                entity.setHospitalName(doctor.getHospName());
                entity.setDoctorName(doctor.getDrName());
            }
        }
        medicalTestService.update(entity);
        return ApiRes.SUCCESS;
    }


    /**
     * 删除检验项目信息
     * @param id 序号
     * @return
     */
    @ApiOperation(value = "删除检验项目信息", notes = "删除检验项目信息")
    @RequestMapping(value = "/deleteTest", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes deleteTest(@ApiParam(required = true, name = "id", value = "序号")
                              @RequestParam("id") Integer id) {
        medicalTestService.delete(id);
        return ApiRes.SUCCESS;
    }


    /**
     * 跳转至检验项目更多页面
     */
    @ApiOperation(value = "检验项目更多页面", notes = "检验项目更多页面")
    @RequestMapping(value = "/toMoreTest", method = RequestMethod.GET)
    public String toMoreTest() {
        return "admin/archives/moreTest";
    }

    @ApiOperation(value = "项目列表", notes = "项目列表")
    @RequestMapping(value = "/searchMedicalProject", method = RequestMethod.POST)
    @ResponseBody
    public MsgVo searchMedicalProject(MedicalProjectEntity entity, @ModelAttribute("pageParam")PageParam pageParam){
        List<MedicalProjectEntity> list = medicalProjectService.queryList(entity, pageParam);
        int count = medicalProjectService.queryTotal(entity);
        return new MsgVo(list,count,count,pageParam.getsEcho());
    }

    /**
     * 跳转至检验项目新增页面-钱湖
     */
    @ApiOperation(value = "检验项目新增页面-钱湖", notes = "检验项目新增页面-钱湖")
    @RequestMapping(value = "/qianhu/toAddTest", method = RequestMethod.GET)
    public String toAddQianhuTest() {
        return "bus/boneLoose/qianhu/addTest";
    }

}
